package com.ganjiangps.huochetou.common.util;

import java.util.Calendar;
import java.util.Random;

public class StringUtil {

	/**
	 * 生成订单编号 16位
	 *
	 * @param
	 * @return 字符串
	 */
	public static String getNo16() {
		String getNo = getNo();
		return getNo.substring(0, getNo.length() - 4);
	}

	/**
	 * 生成订单编号
	 *
	 * @param
	 * @return 字符串
	 */
	public static String getNo() {
		Calendar c = Calendar.getInstance();
		String year = String.valueOf(c.get(Calendar.YEAR));
		String month = String.valueOf(c.get(Calendar.MONTH) + 1);
		String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
		String hours = String.valueOf(c.get(Calendar.HOUR_OF_DAY));
		String m = String.valueOf(c.get(Calendar.MINUTE));
		String s = String.valueOf(c.get(Calendar.SECOND));
		if (month.length() == 1) {
			month = "0" + month;
		}

		if (day.length() == 1) {
			day = "0" + day;
		}
		if (hours.length() == 1) {
			hours = "0" + hours;
		}
		if (m.length() == 1) {
			m = "0" + m;
		}
		if (s.length() == 1) {
			s = "0" + s;
		}
		return year + month + day + hours + m + s + getN(6);
	}

	/**
	 * 获取4-8位随机数
	 *
	 * @return
	 */
	public static int getN(int num) {
		Random random = new Random();
		int number = 0;
		switch (num) {
		case 4:
			number = random.nextInt(8999) + 1000;
			break;
		case 5:
			number = random.nextInt(89999) + 10000;
			break;
		case 6:
			number = random.nextInt(899999) + 100000;
			break;
		case 7:
			number = random.nextInt(8999999) + 1000000;
			break;
		case 8:
			number = random.nextInt(8999999) + 10000000;
			break;
		default:
			number = random.nextInt(8999999) + 1000000;// 默认7位
			break;
		}
		return number;
	}

	public static boolean isNotEmpty(Object str) {
		return !isEmpty(str);
	}

	public static boolean isEmpty(Object str) {
		return str == null || "".equals(str) || String.valueOf(str).length() == 0
				|| String.valueOf(str).matches("\\s*");
	}

}
